package top.superflow.db.orientdb

import scala.collection.JavaConverters._
import scala.collection.mutable.Map

import com.orientechnologies.orient.core.metadata.schema.OSchema

import top.superflow.db.api.DBEntity
import top.superflow.db.api.DBSchema
import top.superflow.exception.RequestUnsupportedException
import top.superflow.verify.Ensure

class OrientSchema (realSchema : OSchema) extends DBSchema with Ensure{
  
  notNull(realSchema, "The argument realSchema should not be null.")
  
  val schema = realSchema
  
  val builtInClass = List("OUser","ORole","OSequence","E","OSchedule","OTriggered","OIdentity","OFunction","ORestricted","V")
  

  def createClass (target : Any) : OrientEntity = {
    target match {
      case x : Class[_] => {
        val realClass = this.schema.createClass(x.getSimpleName)
        
        new OrientEntity(realClass)
      }
      
      case _ => throw new RequestUnsupportedException(target)
      
    }
  }
  
  def existsClass (target : Any) : Boolean = {
    target match {
      case x :Class[_] => {
        this.schema.existsClass(x.getSimpleName)
      }
      case _ => false
    }
  }
  
  def dropClass (target : Any) = {
    target match {
      case x : Class[_] => this.schema.dropClass(x.getSimpleName)
      case y : String => this.schema.dropClass(y)
      case _ => throw new RequestUnsupportedException(target)
    }
  }
  
  def getDBEntity (target : Any): DBEntity = {
    target match {
      case x :Class[_] => {
        new OrientEntity(this.schema.getClass(x.getSimpleName))
      }
      case _ => throw new RequestUnsupportedException(target)
    }
    
  }
  
  def entityMaps() : Map[String, DBEntity] = {
    val entityMap = Map[String, DBEntity]()
    if(this.schema != null){
      println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
      for(originalEntity <- this.schema.getClasses.asScala){
        if(! (builtInClass contains originalEntity.getName))
        entityMap += (originalEntity.getName -> new OrientEntity(originalEntity))
      }
    }
    entityMap
  }
  
}